package ui;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DatePicker extends JPanel
{

	public DatePicker()
	{
		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[]{30, 30, 30, 30, 30, 30, 30, 30};
		gbl.rowHeights = new int[]{30,30,30,30,30,30,30};
		gbl.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
		gbl.rowWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
		this.setLayout(gbl);
		this.drawDatePicker();
	}

	private JPanel drawDatePicker()
	{
		this.drawDayTitles();
	
		int todayDate;
		int firstDayOfWeek;
		int lastDayOfMonth;

		// expand to match months listing
		Calendar cal = Calendar.getInstance();
		Date today = new Date();
		cal.setTime(today);
		todayDate= cal.get(Calendar.DATE);
		cal.get(Calendar.MONTH);
		cal.get(Calendar.YEAR);
		cal.set(Calendar.DATE, 1);

		firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
		lastDayOfMonth = cal.get(Calendar.DATE);
		
		drawDates(firstDayOfWeek, lastDayOfMonth, todayDate);
		
		
		return null;
	}
	private void drawDates(int firstDayOfWeek, int lastDayOfMonth, int todayDate)
	{
		int x=firstDayOfWeek-1;
		int y=1;
		
		for(int i = 1; i<=lastDayOfMonth;i++)
		{
			boolean isToday = false;
			if(y<7)
			{
				if(x>7)
				{
					x=1;
					y++;
				}
				GridBagConstraints gbc = new GridBagConstraints();
				gbc.anchor = GridBagConstraints.CENTER;
				gbc.gridx = x;
				gbc.gridy = y;
				gbc.fill = GridBagConstraints.BOTH;
				gbc.insets = new Insets(0,0,0,0);
				String text = ""+i+""; 
				if(i==todayDate)
				{
					isToday = true;
				}
				this.add(this.button(text, isToday),gbc);
				x++;
				
			}
		}
	}
	private void drawDayTitles()
	{
		for(int gridx=1 ;gridx<=7; gridx++)
		{
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.CENTER;
			gbc.gridx = gridx;
			gbc.gridy = 0;
			//gbc.fill = GridBagConstraints.BOTH;
			gbc.insets = new Insets(0,0,0,0);
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.DAY_OF_WEEK, gridx+1);
			String text = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());
			this.add(this.title(text),gbc);
		}
	}
	private JLabel title(String text)
	{
		JLabel l = new JLabel();
		l.setText(text);
		return l;
	}
	private JButton button(String text, boolean isToday)
	{
		JButton b = new JButton(text);
		if(isToday)
		{
			b.setBackground(Color.RED);
		}
		b.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//to do
			}
		});
		return b;
	}
	
	
}
